﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace com.uplus.rabbitmqhelper
{
    public  abstract class RabbitMQBase
    {
        #region 定义变量
        /// <summary>
        /// 链接器
        /// </summary>
        public IConnection connection = null;

        /// <summary>
        /// 链接通道
        /// </summary>
        public IModel channel = null;
        #endregion

        #region 创建链接工厂
        /// <summary>
        /// 创建链接工厂
        /// </summary>
        /// <param name="ip">RabbitMQ服务器的IP地址</param>
        /// <param name="Port">端口号</param>
        /// <param name="UserName">用户名</param>
        /// <param name="UserPwd">密码</param>
        /// Creator:LM  Time:2018年3月21日14:58:22
        public void CreatConnectionFactory(string ip, int port, string userName, string userPwd)
        {
            try
            {
                var factory = new ConnectionFactory();
                factory.HostName = ip;
                factory.Port = port;
                factory.UserName = userName;
                factory.Password = userPwd;
                connection = factory.CreateConnection();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 创建链接工厂和通道
        /// <summary>
        /// 创建链接工厂和通道
        /// </summary>
        /// <param name="ip">RabbitMQ服务器的IP地址</param>
        /// <param name="Port">端口号</param>
        /// <param name="UserName">用户名</param>
        /// <param name="UserPwd">密码</param>
        /// Creator:LM  Time:2018年3月21日14:58:22
        public void CreatConnectionFactoryAndChanel(string ip, int port, string userName, string userPwd)
        {
            try
            {
                var factory = new ConnectionFactory();
                factory.HostName = ip;
                factory.Port = port;
                factory.UserName = userName;
                factory.Password = userPwd;
                connection = factory.CreateConnection();
                channel = connection.CreateModel();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool CreatConnectionFactoryAndChanelForBool(string ip, int port, string userName, string userPwd)
        {
            bool isConnet = false;
            try
            {
                var factory = new ConnectionFactory();
                factory.HostName = ip;
                factory.Port = port;
                factory.UserName = userName;
                factory.Password = userPwd;
                connection = factory.CreateConnection();
                channel = connection.CreateModel();
                isConnet = connection.IsOpen;
            }
            catch (Exception ex)
            {
                isConnet = false;
            }

            return isConnet;
        }
        #endregion

        #region 创建交换机（需要先创建链接工厂）
        /// <summary>
        /// 创建交换机（需要先创建链接工厂）
        /// </summary>
        /// <param name="exchange">交换机名称</param>
        /// <param name="type">交换机类别：direct，fanout，topic</param>
        /// <param name="durable">是否持久化</param>
        /// <param name="autoDelete">是否自动删除</param>
        /// Creator:LM  Time:2018年3月21日15:06:30
        public void CreatExchange(string exchange, string type, bool durable, bool autoDelete)
        {
            try
            {
                if (channel == null)
                {
                    channel = connection.CreateModel();
                }
                channel.ExchangeDeclare(exchange, type, durable: durable, autoDelete: autoDelete);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 创建消息队列并绑定在交换机上（需要先创建交换机，或者保证交换机已存在，否则会报错）,可以多次调用来创建多个队列
        /// <summary>
        /// 创建消息队列并绑定在交换机上（需要先创建交换机，或者保证交换机已存在，否则会报错）,可以多次调用来创建多个队列
        /// </summary>
        /// <param name="queueName">队列名称</param>
        /// <param name="exchange">交换机名称</param>
        /// <param name="durable">是否持久化</param>
        /// <param name="autoDelete">是否自动删除</param>
        /// <param name="routingKey">路由键</param>
        /// Creator:LM  Time:2018年3月21日15:19:27
        public void CreatAndBindQueue(string queueName, string exchange, bool durable, bool autoDelete, string routingKey)
        {
            try
            {
                if (channel != null)
                {
                    channel.QueueDeclare(queueName, durable: durable, exclusive: false, autoDelete: autoDelete);
                    channel.QueueBind(queueName, exchange, routingKey);                   
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

    }
}
